package com.daxiasoftware.xxx.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import com.daxiasoftware.xxx.service.MyUserDetailsService;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private MyUserDetailsService myUserDetailsService;
	@Autowired
	private PasswordEncoder passwordEncoder;
	
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/" , 
                		"/alipay/*",
                		"/res/*/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                //.loginPage("/admin/login")
            .loginPage("/admin/login")
            .loginProcessingUrl("/login")
            //.successForwardUrl("/admin/index")
            .defaultSuccessUrl("/admin/index", true)
                //.loginProcessingUrl("/admin/doLogin")
                .permitAll()
                //.failureForwardUrl("/admin/login?error")
                .and()
            .logout()
                .permitAll();
        http.csrf().disable();
        http.headers().frameOptions().sameOrigin();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder);
    }

}